using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace HEFramework
{
    /// <summary>
    ///
    /// UI滑动条行为基类
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-02-17 16:51:49
    /// ----------------------------------------
    /// </summary>
    [RequireComponent(typeof(Slider))]
    public class UISliderBehaviourBase : MonoBehaviour, IUISliderBehaviour, IPointerEnterHandler, IPointerClickHandler, IPointerExitHandler
    {
        protected Slider slider;

        protected Action enterEvent;

        protected Action<int> valueEvent;

        protected Action exitEvent;


        public Slider Slider => slider;


        public void Awake()
        {
            if (slider == null)
            {
                slider = GetComponent<Slider>();
                slider.onValueChanged.AddListener((_) =>
                {
                    int value = (int)_;
                    valueEvent?.Invoke(value);
                });
            }
        }

        public Transform GetTransform()
        {
            return slider.transform;
        }

        /// <summary>
        /// 返回对象
        /// </summary>
        /// <returns></returns>
        public GameObject GetGameObject()
        {
            return slider.gameObject;
        }

        public void OnEnter()
        {
            enterEvent?.Invoke();
        }

        public void OnValue(int _value)
        {
            valueEvent?.Invoke(_value);
        }

        public void OnExit()
        {
            exitEvent?.Invoke();
        }


        public void OnPointerEnter(PointerEventData _eventData)
        {
            OnEnter();
        }

        public void OnPointerClick(PointerEventData _eventData)
        {
        }

        public void OnPointerExit(PointerEventData _eventData)
        {
            OnExit();
        }


        public IUISliderBehaviour RegisterEvent(Action _enter = null, Action<int> _value = null, Action _exit = null)
        {
            enterEvent = _enter;
            valueEvent = _value;
            exitEvent = _exit;
            return this;
        }

        public IUISliderBehaviour RegisterEnterEvent(Action _callback)
        {
            enterEvent = _callback;
            return this;
        }

        public IUISliderBehaviour RegisterOnValueEvent(Action<int> _callback)
        {
            valueEvent = _callback;
            return this;
        }

        public IUISliderBehaviour RegisterExitEvent(Action _callback)
        {
            exitEvent = _callback;
            return this;
        }
    }
}